package edu.mit.media.fv.aqwidgets
{
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.icons.*;
	import edu.mit.media.fv.layers.*;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.text.*;
	
	public class AQGroupEditor extends AQWidget
	{
		var layer:Layer;
		
		private var space:FluidSpace;
		private var groups:Array = new Array();
		private var blocks:Object = new Object();
		public function AQGroupEditor(space:FluidSpace)
		{
			super();
			this.space = space;
			this.layer = new GroupLayer(space);
			this.layer.layerWidth = 600;
			this.layer.layerHeight = 280;
			this.layer.x = 10;
			this.layer.y = 100;
			this.layer.active = false;
			this.addChild(this.layer);
			
			FluidEventHandler.addEventListener(UpdateEvent.ADD_GROUP, addGroup);
			FluidEventHandler.addEventListener(UpdateEvent.ADD_SERVICE, addService);
			FluidEventHandler.addEventListener(UpdateEvent.ADD_USER, addUser);
			
			FluidEventHandler.addEventListener(WidgetEvent.DISPLAY_GROUP_VIEW, displayWidget);
		}
		public override function getBlockContainers():Array{
			return [this.layer];
		}
		private function processCreateGroup(event:MouseEvent):void{
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_CREATE_GROUP;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
		private function processRemoveGroup(event:MouseEvent):void{
			var block:Block = space.selectedBlock;
			if(block != null){		
				//remove group from layer
				this.layer.removeBlock(block);
				
				//notify engine that group has been removed
				var data:Object = new Object();
				data.command = FluidConstants.REQUEST_REMOVE_GROUP;
				data.id = block.model.id;
				var e:FVNetworkEvent = new FVNetworkEvent(data);
				FluidEventHandler.dispatchEvent(e);
				
				//notify the rest of system that block has been removed				
				FluidEventHandler.dispatchEvent(new UpdateEvent(UpdateEvent.DELETE, block.model.id));
		
			}
		}
		public function displayWidget(event:WidgetEvent = null):void{
			FluidEventHandler.dispatchEvent(new WidgetEvent(WidgetEvent.DISPLAY_WIDGET, this, "Group"));
		}
		public function addUser(event:UpdateEvent):void{
			var model:Model = Model.getModel(event.id);
			if(model != null){
				this.addBlock(model.getGroupInstance());
			}
		}
		public function addService(event:UpdateEvent):void{
			var model:Model = Model.getModel(event.id);
			if(model != null){
				this.addBlock(model.getGroupInstance());
			}
		}
		
		public function addGroup(event:UpdateEvent):void{
			var model:Model = Model.getModel(event.id);
			if(model != null){
				this.addBlock(model.getGroupInstance());
			}
		}
		public function addBlock(block:Block):void{
			this.layer.addBlock(block);
			this.blocks[block.model.id] = block;
		}
		
	}
}